<?php
include_once("../includes/common.php");
include_once("../class/User.class.php");
include_once("../class/Explore.class.php");
include_once("../class/Comments.class.php");
require_once("../islogin.php");

if ($isadmin!=1){
    header('Location: /');
    exit;
}

$menuavatar = !$user_info["message"]["avatar"] ? "/data/avatar/default.jpg" : $user_info["message"]["avatar"];
$pageName = "AI评论生成";
$num = 0;
$explore = new Explore();
$comments = new Comments();

if($_GET['nid']&&!isUid($_GET['nid'])){
    exit('error nid');
}
if($_GET['nid']){
    $res = json_decode($explore->getIdNotes($_GET['nid']),true);
    if(!$res){
        header('Location: aibot.php');
        exit('不存在的nid');
    }
    $num = isset($_GET['num']) ? intval($_GET['num']) : 0;
    if($num<=0){
        header('Location: aibot.php');
        exit('不能为0');
    }
    //$inputnum = $num - 1;      //要减掉一个输入框



    if ($_SERVER['REQUEST_METHOD'] === 'POST') {
        if (isset($_POST['action'])) {
            if ($_POST['action'] == 'generate') {
                // 处理生成AI评论的逻辑
                $aicomments = xlsaiComments($res,$num);

                //var_dump($aicomments);
            } elseif ($_POST['action'] == 'send') {
                $ai_cm_num = count($_POST["comments"]);
                $random_bot = $user->randomBot($ai_cm_num);
                for ($sendn=0; $sendn<$ai_cm_num; $sendn++) {
                    $comments->sendComments($random_bot[$sendn]["user_id"], $_GET['nid'], $_POST["comments"][$sendn],1);
                }
                $referrer = $_SERVER['HTTP_REFERER'];
                header("Location: $referrer");
                exit();
            }
        }
    }

}




//腾讯元器 - https://yuanqi.tencent.com/

function xlsaiComments($res,$num=1)
{
    $assistant_id = "xxx";                 // 小蓝书神评论Bot - 智能体ID
    $token = "xxx";              // 小蓝书神评论Bot - Token

    $message = [
        'note_id'=>$res['note_id'],
        "note_title"=>$res['title'],
        "note_content"=>$res['content'],
        "note_keywrods"=>$res['tags'],
        "need_comments"=>$num,
    ];
    $message = json_encode($message,JSON_UNESCAPED_UNICODE);

    $response = json_decode(callTencentAiApi($assistant_id, $token, $message),true);

    if($response["error"]){
        return '腾讯API返回error：'.$response["error"]['message'];
    }else{
        $aires = json_decode($response["choices"][0]["message"]["content"],true);
        return $aires;
    }
}


?>
<!DOCTYPE html>
<html lang="zh-CN">
<head>

    <?php require_once('head.php'); ?>
    <style>
        .text-del-line{
            text-decoration: line-through;
        }
        .side-overlay p{
            position: absolute;
            top: 50%;
            left: 50%;
            font-size: 18px;
            color: white;
            background-color: #3e80f9;
            padding: 4px 12px;
            border-radius: 10px;
            width: 200px;
            text-align: center;
        }


    </style>
</head>
<body>

<!--==================== Preloader Start ====================-->
<div class="preloader">
    <div class="loader"></div>
</div>
<!--==================== Preloader End ====================-->

<!--==================== Sidebar Overlay End ====================-->
<div class="side-overlay">
    <p>A I 评 论 生 成 中...</p>
</div>
<!--==================== Sidebar Overlay End ====================-->


<?php require_once('menu.php'); ?>
<div class="dashboard-main-wrapper">

    <?php require_once('top_navbar.php'); ?>
    <div class="dashboard-body">
        <!-- My Details Tab start -->
        <div class="tab-pane fade show active" id="pills-details" role="tabpanel" aria-labelledby="pills-details-tab" tabindex="0">
            <div class="card">
                <div class="card-header border-bottom">
                    <h4 class="mb-4"><?php echo $pageName?></h4>
                </div>
                <div class="card-body">
                    <form action="" method="get">
                        <div class="row gy-4">
                            <div class="col-sm-12 col-xs-12">
                                <label for="fname" class="form-label mb-8 h6">笔记noteId</label>
                                <input type="text" required maxlength="100" value="<?php echo $_GET['nid']?>" name="nid" class="form-control py-11" id="fname" placeholder="输入要生成AI评论的笔记noteId">
                            </div>
                            <div class="col-sm-12 col-xs-12">
                                <label for="fname" class="form-label mb-8 h6">生成AI评论的数量</label>
                                <input type="number" required min="1" max="5" value="<?php echo $_GET['num']?>" name="num" class="form-control py-11" id="fname" placeholder="输入生成AI评论的数量(一次最大5条)">
                            </div>
                            <div class="col-12">
                                <div class="flex-align justify-content-end gap-8">
                                    <button type="submit" class="btn btn-main rounded-pill py-9">查询</button>
                                </div>
                            </div>
                        </div>
                    </form>
                </div>
            </div>
        </div>
        <!-- My Details Tab End -->
        <?php if($_GET['nid']){?>

            <div class="card mt-24">
                <div class="card-header border-bottom">
                    <div class="flex-between flex-wrap  gap-16">
                        <div>
                            <h4 class="mb-4">查询结果</h4>
                        </div>
                    </div>
                </div>


                <div class="card-body">
                    <div class="rounded-8 border border-gray-100 p-16 d-flex gap-12">
                        <div class="d-inline-flex w-100 max-w-130 flex-shrink-0">
                            <img src="<?php echo getFirstValidImage($res['image']);?>" alt="" class="rounded-6 cover-img max-w-130 max-h-200">
                        </div>
                        <div>
                            <p class="text-gray-600 text-line-3"><a target="_blank" href="/explore.php?id=<?php echo $res['note_id'];?>" class="<?php echo $res['is_view']==1 ? 'hover-text-main-600':'text-danger hover-text-danger-600 text-del-line' ;?>"><?php echo $res['title'];?></a></p>

                            <?php
                            $check_result = $res['check_result'];

                            switch ($check_result) {
                                case 0:
                                    echo '<span class="text-13 py-2 px-10 rounded-pill bg-warning-50 text-warning-600 mb-16">审核中</span>';
                                    break;
                                case 1:
                                    echo '<span class="text-13 py-2 px-10 rounded-pill bg-success-50 text-success-600 mb-16">审核通过</span>';
                                    break;
                                case 2:
                                    echo '<span class="text-13 py-2 px-10 rounded-pill bg-danger-50 text-danger-600 mb-16">笔记违规</span>';
                                    break;
                                default:
                                    echo '<span class="text-13 py-2 px-10 rounded-pill bg-secondary-50 text-secondary-600 mb-16">未知状态</span>';
                                    break;
                            }
                            echo $res['note_type'] == 1 ? '<span style="margin-left: 10px" class="text-13 py-2 px-10 rounded-pill bg-main-50 text-main-600 mb-16">视频</span>' : '<span style="margin-left: 10px" class="text-13 py-2 px-10 rounded-pill bg-pink-50 text-pink-600 mb-16">笔记</span>';
                            ?>

                            <div class="flex-align gap-8 mt-24">
                                <img src="<?php echo '/' . ltrim($res["avatar"] ?: '/data/avatar/default.jpg', '/'); ?>" alt="" class="w-32 h-32 rounded-circle cover-img">
                                <span class="text-gray-600 text-13"><?php echo $res['nickname'];?></span>

                            </div>
                        </div>
                    </div>
                    <form action="" method="POST">
                        <div class="row gy-4 mt-2">
                            <div class="col-sm-12 col-xs-12">
                                <label for="fname" class="form-label mb-8 h6">评论内容：</label>
                                <input type="text" maxlength="300" value="<?php echo $aicomments["comments"][0]?>" name="comments[]" class="form-control py-11" id="fname" placeholder="输入评论内容">
                            </div>
                            <?php
                            for ($i = 1; $i < $num; $i++) {
                                echo '<div class="col-sm-12 col-xs-12">
                                <input type="text" maxlength="300" value="'.$aicomments["comments"][$i].'" name="comments[]" class="form-control py-11" id="fname_' . $i . '" placeholder="输入评论内容">
                              </div>';
                            }
                            ?>


                            <div class="col-12">
                                <div class="flex-align justify-content-end gap-8">
                                    <button type="submit" name="action" value="generate" class="btn btn-main rounded-pill py-9 toggle-btn">生成AI评论</button>
                                    <button type="submit" name="action" value="send" class="btn btn-main rounded-pill py-9">提交</button>
                                </div>
                            </div>
                        </div>
                    </form>
                </div>
            </div>

        <?php }?>


    </div>

    <?php require_once('footer.php'); ?>
</div>

<?php require_once('foot.php'); ?>


</body>
</html>